#!/bin/bash

projectPath=$(pwd)
scriptPath=$(cd $(dirname $0) && pwd)
projectName="${projectPath##*/}"
templatePath=${scriptPath}/template

source ${scriptPath}/source_tools_whiptail.sh

cd ${projectPath}


scriptPath/git.sh


messagebox "初始化向导" "现在开初始化项目" 10 60 "开始"
#----------------------------------------------------------------
# 第一步，确认项目详细
#----------------------------------------------------------------

value=$(inputbox "项目路径" "在此路径下创建项目" 10 60 "$projectPath" "确定" "取消")
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

value=$(inputbox "项目名称" "您的项目名称" 10 60 "$projectName" "确定" "取消")
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi
projectName=${value}

GOBIN=$(which go)
value=$(inputbox "golang配置" "bin文件路径" 10 60 "$GOBIN" "确定" "取消")
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

GOVERSION=$(${GOBIN} env | grep GOVERSION)
promptbox "GOVERSION" "${GOVERSION}" 10 60 "确定" "取消"
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

GOROOT=$(${GOBIN} env | grep GOROOT)
promptbox "GOROOT" "${GOROOT}" 10 60 "确定" "取消"
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

GOPATH=$(${GOBIN} env | grep GOPATH)
promptbox "GOPATH" "${GOPATH}" 10 60 "确定" "取消"
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

value=$(inputbox "GOPROXY" "是否配置国内代理" 10 60 "https://goproxy.cn,direct")
ret=$?
if [ $ret = 0 ]; then
    messagebox "执行命令" "go env -w GOPROXY=${value}" 10 60 "好的"
    go env -w GOPROXY=${value}
fi

GOPROXY=$(${GOBIN} env | grep GOPROXY)
promptbox "GOPROXY" "${GOPROXY}" 10 60 "确定" "取消"
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

value=$(inputbox "GO111MODULE" "开启模块模式" 10 60 "on")
ret=$?
if [ $ret = 0 ]; then
    messagebox "执行命令" "go env -w GO111MODULE=${value}" 10 60 "好的"
    go env -w GO111MODULE=${value}
fi

GO111MODULE=$(${GOBIN} env | grep GO111MODULE)
promptbox "GO111MODULE" "${GO111MODULE}" 10 60 "确定" "取消"
ret=$?
if [ $ret != 0 ]; then
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

#----------------------------------------------------------------
# 第二步，初始化项目模块
#----------------------------------------------------------------

value=$(inputbox "模块路径" "是否使用此模块路径" 10 60 "ss360.org/${projectName}")
ret=$?
if [ $ret = 0 ]; then
    messagebox "执行命令" "go mod init ${value}" 10 60 "好的"
    go mod init ${value}
else
    messagebox "退出" "退出初始化向导" 10 60 "好的"
    exit 0
fi

echo vendor > .gitignore
echo .idea >> .gitignore
echo .log >> .gitignore
echo __debug_bin >> .gitignore

# 入口文件
if test ! -d ${projectPath}/cmd; then
    mkdir -p ${projectPath}/cmd/${projectName}
    cp ${templatePath}/main.go ${projectPath}/cmd/${projectName}/

    touch ${projectPath}/cmd/入口文件
    echo "入口文件" >${projectPath}/cmd/.gitignore
fi

# 内部代码
if test ! -d ${projectPath}/internal; then
    mkdir -p ${projectPath}/internal/${projectName}
    mkdir -p ${projectPath}/internal/pkg

    touch ${projectPath}/internal/内部代码
    echo "内部代码" >${projectPath}/internal/.gitignore
fi

# 包目录
if test ! -d ${projectPath}/pkg; then
    mkdir -p ${projectPath}/pkg

    touch ${projectPath}/pkg/包目录
    echo "包目录" >${projectPath}/pkg/.gitignore
fi

# 第三方源代码
if test ! -d ${projectPath}/vendor; then
    mkdir -p ${projectPath}/vendor

    touch ${projectPath}/vendor/三方源代码
    echo "三方源代码" >${projectPath}/vendor/.gitignore
fi

# 项目文件
if test ! -d ${projectPath}/.vscode; then
    cp -r ${templatePath}/.vscode ${projectPath}/.vscode
fi

# 忽略文件
if test ! -f ${projectPath}/.gitignore; then
    cp ${templatePath}/.gitignore ${projectPath}/.gitignore
fi

# 说明文件
if test ! -f ${projectPath}/README.md; then
    echo "# ${projectName}" >${projectPath}/README.md
fi

# 配置目录
if test ! -d ${projectPath}/configs; then
    mkdir -p ${projectPath}/configs

    touch ${projectPath}/configs/配置目录
    echo "配置目录" >${projectPath}/configs/.gitignore
fi

# 日志目录
if test ! -d ${projectPath}/logs; then
    mkdir -p ${projectPath}/logs
    touch ${projectPath}/logs/日志目录
    echo "*" >${projectPath}/logs/.gitignore
    echo "!.gitignore" >>${projectPath}/logs/.gitignore
    echo "日志目录" >>${projectPath}/logs/.gitignore
fi

# 测试目录
if test ! -d ${projectPath}/tests; then
    mkdir -p ${projectPath}/tests

    touch ${projectPath}/tests/测试目录
    echo "测试目录" >${projectPath}/tests/.gitignore
fi

# 接口文档
if test ! -d ${projectPath}/api; then
    mkdir -p ${projectPath}/api

    touch ${projectPath}/api/接口文档
    echo "接口文档" >${projectPath}/api/.gitignore
fi

# 静态文件
if test ! -d ${projectPath}/assets; then
    mkdir -p ${projectPath}/assets

    touch ${projectPath}/assets/静态文件
    echo " 静态文件" >${projectPath}/assets/.gitignore
fi



# Web资源
if test ! -d ${projectPath}/web; then
    mkdir -p ${projectPath}/web

    touch ${projectPath}/web/web资源
    echo "web资源" >${projectPath}/web/.gitignore
fi

#----------------------------------------------------------------
# 常用模块安装，初始化项目模块
#----------------------------------------------------------------

# 日志相关库
go get github.com/sirupsen/logrus
go get github.com/lestrrat-go/file-rotatelogs

# 配置相关库
go get gopkg.in/yaml.v2
go get gopkg.in/ini.v1

# mysql库
go get github.com/go-sql-driver/mysql

# mqtt库
go get github.com/eclipse/paho.mqtt.golang

# http库
go get -u github.com/gin-gonic/gin


# 其他
go get golang.org/x/sync/errgroup

# json
go get github.com/bitly/go-simplejson


# 同步到vendor文件夹中
go mod vendor
